﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace TemplatesPrint.Tools
{
    public class IpTools
    {
        public static string GetIpAddress()
        {
            string retip = "";
            string name = Dns.GetHostName();
            IPAddress[] ipadrlist = Dns.GetHostAddresses(name);
            if (ipadrlist.Count() > 0)
            {
                foreach (var ip in ipadrlist)
                {
                    retip = ip.ToString() + "; " + retip;
                }

            }
            return retip;
        }
        /// summary 
        /// 获取网卡硬件地址
        /// /summary 
        /// returns mac地址 /returns 
        public static string GetMacAddress()
        {
            try
            {
                //获取网卡硬件地址
                string mac = "";
                ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    if ((bool)mo["IPEnabled"])
                    {
                        mac = mo["MacAddress"].ToString();

                        break;
                    }
                }
                moc = null;
                mc = null;
                return mac;
            }
            catch
            {
                return "";
            }
        }
        
    }
}
